import numpy as np
from math import sqrt
class StandardScaler:
    def __init__(self):
        self.mean_=None
        self.scale_=None
    def fit(self,X):
        assert X.ndim==2#确保是2维
        self.mean_=np.array([np.mean(X[:,i]) for i in range(X.shape[1])])
        self.scale_=np.array([np.std(X[:,i]) for i in range(X.shape[1])])    
        return self
    def transform(self,X):
        assert  X.ndim==2
        assert self.mean_ is not None and self.scale_ is not None
        resX=np.empty(X.shape,dtype=float)#置空矩阵，便于传入归一化结果
        """归一化处理【均值方差归一化】"""
        for col in range(X.shape[1]):
            resX[:,col]=(X[:,col]-self.mean_(X[:,col]))/self.scale_(X[:,col])

        return resX
